package view.editor;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;

import view.Displayable;
import view.View;
import controller.editor.AddEntityToRelationController;

public class AddEntityToRelationView extends View<AddEntityToRelationController> implements Displayable{

	private JFrame frame;
	private AddEntityToRelationController controller;
	@SuppressWarnings("rawtypes")
	JList list;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public AddEntityToRelationView(String[] entities) {
		list = new JList(entities);
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame("Agregar entidad a relacion");
		frame.setBounds(100, 100, 450, 300);
		frame.getContentPane().setLayout(null);
		
		list.setBounds(149, 29, 132, 172);
		frame.getContentPane().add(list);
		
		JButton btnTerminar = new JButton("Terminar");
		btnTerminar.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				getController().handleButtonTerminar();
			}
		});
		btnTerminar.setBounds(159, 213, 117, 25);
		frame.getContentPane().add(btnTerminar);
	}

	@Override
	public void show() {
		frame.setVisible(true);
	}

	@Override
	public void hide() {
		frame.setVisible(false);		
	}

	@Override
	public AddEntityToRelationController getController() {
		return this.controller;
	}

	@Override
	public void setController(AddEntityToRelationController controller) {
		this.controller = controller;
	}

	@SuppressWarnings("deprecation")
	public List<String> getEntitiesList(){
		List<String> strings = new ArrayList<String>();
		for (Object obj: this.list.getSelectedValues()) {
			strings.add((String)obj);
		}
		return strings;
	}
	
}
